<?php

namespace App\BO\OrderStatus\State\Report;

use App\BO\OrderStatus\OrderStateContext;
use App\BO\OrderStatus\OrderStateDefinition;
use App\BO\OrderStatus\State\AOrderState;
use App\BO\OrderStatus\State\DistributingState;
use App\Entities\OrderModel;
use App\Entities\OrderReportEntity;
use App\Entities\OrderReportModel;

class ReportedState extends AOrderState
{
    protected $desc = '订单已报备';

    /**
     * @inheritDoc
     */
    public function cleanerCancelReport()
    {
        // 撤销报备，订单都流转成待上门状态
        $this->toWaitingService();
    }

    /**
     * @inheritDoc
     */
    public function adminAgreeReport()
    {
        $orderEntity = $this->getOrder();

        // 中台审核通过：若是改期的报备订单，流转成待上门，上门时间更改为客户要求的上门时间
        // 若是其它原因导致的报备订单，审核通过，流转成待分配状态
        $orderReportEntity = OrderReportEntity::findStateInitByOrderId($orderEntity->getId());
        if (OrderReportModel::CHANGE_SERVICE_DATE_REASON_ID == $orderReportEntity->getReasonId()) {
            $this->toWaitingService();
        } else {
            $this->backToDistributing();
        }
    }

    /**
     * @inheritDoc
     */
    public function adminRejectReport()
    {
        // 中台审核不通过，订单流转成待上门状态
        $this->toWaitingService();
    }

    /**
     *  变成待分配状态
     */
    private function backToDistributing()
    {
        $orderEntity = $this->getOrder();

        $orderEntity->setOrderState(OrderStateDefinition::STATE_WAITING_DISTRIBUTION);
        $orderEntity->setShipStatus(OrderModel::SHIP_STATUS_DISTRIBUTION);
        $orderEntity->save();

        $this->stateContext->setOrderState(OrderStateContext::getInstance(DistributingState::class));
    }
}
